import os
import os.path

rootdir = r'/Users/zhangyanqiang/Documents/workspace/e-commerce/axe/axe-api'


def replace(path,filename):
    try:
        oldfile = open(path, 'rb+')
        newfile = open(path, 'ab+')

        old = b'\r'
        new = b'\r\n'

        data = b''
        while (True):
            data = oldfile.read(200)
            newData = data.replace(old, new)
            newfile.write(newData)
            if len(data) < 200:
                break
        newfile.close()
        oldfile.close()

        #os.remove(path)
        os.rename('$' + filename,  '/' + filename)
    except IOError as e:
        print(e)


for parent, dirnames, filenames in os.walk(rootdir):
    for filename in filenames:
        if filename[-5:] != '.java' :  # 只替换java文件
            print
            "file:", filename
            continue
        replace(parent+"/" + filename, filename)